﻿using HomagGroup.BaseUI.Properties;
using HomagGroup.Base.UI.Themes.Resources;

using System;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Brush = System.Windows.Media.Brush;
using Brushes = System.Windows.Media.Brushes;
using Icon = HomagGroup.Base.UI.Themes.Resources.Icon;
using Image = HomagGroup.Base.UI.Themes.Resources.Image;

namespace HomagChina.CCT
{
    // 扩展类
    public static class ProcessResourceInternal
    {
        private static ResourceDictionary iconDictionary;

        static ProcessResourceInternal()
        {
            iconDictionary = new ResourceDictionary();
            iconDictionary.Source = new Uri("pack://application:,,,/HomagGroup.BaseUI;component/UIHelper/Icons/Icons.xaml");
        }

        // 获取Brush类型的资源
        public static Brush GetResourceBrush(string key) => iconDictionary.Contains(key) ? iconDictionary[key] as Brush : Brushes.Transparent;

        // 将Thems.Resources.Icon转化为Brush类型的资源
        public static Brush GetIconBrush(Icon icon, ImageDecoration imageDecoration = ImageDecoration.None, OverlayIcon overlayIcon = OverlayIcon.None, Boolean crossedOut = false) => new Image { Icon = icon, Decoration = imageDecoration, Overlay = overlayIcon, CrossedOut = crossedOut }.ToBrush();

        //使用16进制颜色转化为brush
        public static Brush GetHexadecimalColor(string color) => (Brush)new BrushConverter().ConvertFromString(color);

        //根据图片路径获取ImageBrush

        public static ImageBrush CraeteImageBrushFill(string imagePath)
        {
            ImageBrush imageBrush = new ImageBrush();
            BitmapImage bitmapImage = new BitmapImage(new Uri(imagePath, UriKind.RelativeOrAbsolute));
            imageBrush.ImageSource = bitmapImage;
            return imageBrush;
        }

        //根据bitMap 转ImageBrush

        public static ImageBrush BitMapConvertsImageBrush(string MachineImagePath)
        {
            ImageBrush imageBrush = new ImageBrush();
            var obj = ImageResource.ResourceManager.GetObject(MachineImagePath);
            if (obj != null)
            {
                Bitmap bitmap = obj as Bitmap;
                MemoryStream ms = new MemoryStream();
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = ms;
                bitmapImage.EndInit();
                imageBrush.ImageSource = bitmapImage;
            }
            return imageBrush;
        }

        //获取图片的宽
        public static double GetImageWidth(string imagePath)
        {
            Bitmap bitmap = new Bitmap(imagePath);
            return bitmap.Width;
        }

        //获取图片的高
        public static double GetImageHeight(string imagePath)
        {
            Bitmap bitmap = new Bitmap(imagePath);
            return bitmap.Height;
        }

        //获取图片名称
        public static string GetImageName(string imagePath)
        {
            return Path.GetFileName(imagePath);
        }

        #region DrawingImage 转化为 Icon 目前有问题 后续有需要再添加

        // public static Icon GetResourceIcon(string key)
        // {
        // if (iconDictionary.Contains(key))
        // {
        // object iconObject = iconDictionary[key];
        // if (iconObject is Icon)
        // {
        // return iconObject as Icon;
        // }
        // else if (iconObject is DrawingImage drawingImage)
        // {
        // ImageSource imageSource = drawingImage;
        // return imageSource.ToIcon();
        // }
        // }
        // return null;
        // }

        #endregion DrawingImage 转化为 Icon 目前有问题 后续有需要再添加
    }
}